<?php
/*
 * @分销商城
 * @功能说明：功能说明
 * @更新说明：暂无更新
 * @文件名 ProfitController.class.php
 * @编码 UTF-8
 * @创建时间 2016-3-25 下午3:52:23
 * @创建人 XING
 */

namespace wap\Controller;
use Think\Controller;

class ProfitController extends Controller {
    /*
	 * 分销商城_分销商城收益
	 * @date: 2016-3-25 下午3:53:24
	 * @editor: XING
	 */
	public function salerProfit(){
		$rulesArr = array(
					'1' => array(
							'160','500','300'
						),
					'2' => array(
							'188','788','350'
						),
					'3' => array(
							'188','888','588'	
						)
				);
		$zjProfit = array(
				'158','188','88'
			);
		$levelName = array(
				'推荐用户升级嗨呗专员 提成收益','推荐直升嗨呗经理 提成收益','直属下级升级嗨呗经理 提成收益'
			);
		$getProFits = M('CustInlevel')
					->where('cash_at = 0 AND biz_cust_salelevel > 0')->select();
		if($getProFits){
			foreach($getProFits AS $val){

				$mainProfit = M('CustBottler');
				$putProfit = $mainProfit->lock(true)->add(array(
							'finance_money' => $rulesArr[$val['biz_cust_salelevel']][$val['type']],
							'cust_id' => $val['biz_cust_id'],
							'money_remark' => $levelName[$val['type']],
							'is_waste' => '0',
							'add_at' => time()
						));
				if($putProfit){
					$levelMain = M('CustInlevel');
					$signSuccess = $levelMain->lock(true)
								 ->where(array('id'=>$val['id']))->setField(array(
								 			'cash_at' => time()
								 		));
					if($signSuccess){
						//总监待遇
						$zjId = self::findZj($val['biz_cust_id'],true);
						if($zjId){
							$zjTcheng = $mainProfit->lock(true)->add(array(
									'finance_money' => $zjProfit[$val['type']],
									'cust_id' => $zjId,
									'money_remark' => '嗨呗总监 团队育成 提成收益',
									'is_waste' => '0',
									'add_at' => time()
							));
							if($zjTcheng){
								$mainProfit->commit();
								$levelMain->commit();
							}else{
								$mainProfit->rollback();
								$levelMain->rollback();
							}
						}else{
							$levelMain->commit();
						}
					}else{
						$mainProfit->rollback();
						$levelMain->rollback();
					}
				}else{
					$mainProfit->rollback();
				}
			}
		}
	}
	
    /*
	 * 分销商城_我要找总监
	 * @date: 2016-3-25 下午6:37:56
	 * @editor: XING
	 */
	public function findZj($uid,$isZj=true){
		$getSalerData = M('CustEntity')
					  ->field('salelevel,parent_id')->where(array('cust_id'=>$uid))->find();
		if($getSalerData['salelevel'] == '3'){
			if($isZj==true){
				return 0;
			}else{
				return $uid;
			}
		}else if(!$getSalerData['parent_id']){
			return 0;
		}else{
			return self::findZj($getSalerData['parent_id'],false);
		}
	}

    /*
	 * 分销商城_商品收益
	 * @date: 2016-3-25 下午3:53:57
	 * @editor: XING
	 */
	public function goodsProfit(){
		$getGoodsProfit = M('GoodsProfit')
						//->where('is_profit = 3 AND success_at > 0 AND pay_at > 0')->select();
					    ->where('is_profit = 3 AND pay_at > 0 AND success_at < '.(time()-7*24*3600))->select();
		$profitData = array();
		if($getGoodsProfit){
			foreach($getGoodsProfit AS $vals){
				if($vals['profit'] > 0){
					$mainProfit = M('CustBottler');
					$putProfit = $mainProfit->lock(true)->add(array(
							'finance_money' => $vals['profit'] * $vals['goods_nums'],
							'cust_id' => $vals['biz_cust_id'],
							'money_remark' => '订单号 '.$vals['order_id'].'店铺收益提成',
							'is_waste' => '0',
							'add_at' => time()
						));
					$profitData[] = array(
						'cust_id' => $vals['cust_id'],
						'profit_cust_id' => $vals['biz_cust_id'],
						'order_id' => $vals['order_id'],
						'profit_money' => $vals['profit'] * $vals['goods_nums'],
						'profit_remark' => '订单号 '.$vals['order_id'].'店铺收益提成',
						'type' => '1'
					);
					if($vals['zj_profit'] > 0){
						$zjProfit = $mainProfit->lock(true)->add(array(
							'finance_money' => $vals['zj_profit'] * $vals['goods_nums'],
							'cust_id' => $vals['zj_cust_id'],
							'money_remark' => '嗨呗总监团队销售业绩利润提成',
							'is_waste' => '0',
							'add_at' => time()
						));
						if($zjProfit){
							$profitData[] = array(
								'cust_id' => $vals['cust_id'],
								'profit_cust_id' => $vals['zj_cust_id'],
								'order_id' => $vals['order_id'],
								'profit_money' => $vals['zj_profit'] * $vals['goods_nums'],
								'profit_remark' => '嗨呗总监团队销售业绩利润提成',
								'type' => '1'
							);
						}
					}
					if($putProfit){
						$pushFit = M('GoodsProfit');
						$effectFit = $pushFit->lock(true)
								   ->where(array('id'=>$vals['id']))->setField(array(
								   			'is_profit' => '1',
								   			'update_at' => time()
								   		));
						if($effectFit){
							$pushFit->commit();
							$mainProfit->commit();
							inProfit($profitData);
						}else{
							$pushFit->rollback();
							$mainProfit->rollback();
						}
					}else{
						$mainProfit->rollback();
					}
				}
			}
		}
	}
}